﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MileageTracker
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void pictureBox_Paint(object sender, PaintEventArgs e)
        {
            DrawLabel(labelDistance, pictureBox.CreateGraphics());
            DrawLabel(labelFuel, pictureBox.CreateGraphics());
            DrawLabel(labelPrice, pictureBox.CreateGraphics());
            DrawLabel(labelTank, pictureBox.CreateGraphics());
        }

        private void DrawLabel(Label label, Graphics grx)
        {
            if (label.TextAlign == ContentAlignment.TopLeft)
            {

                grx.DrawString(label.Text, label.Font, new SolidBrush(label.ForeColor), label.Bounds);

            }

            else if (label.TextAlign == ContentAlignment.TopCenter)
            {

                SizeF size = grx.MeasureString(label.Text, label.Font);

                int left = this.Width / 2 - (int)size.Width / 2;

                Rectangle rect = new Rectangle(left, label.Top, (int)size.Width, (int)label.Height);

                grx.DrawString(label.Text, label.Font, new SolidBrush(label.ForeColor), rect);

            }

            else if (label.TextAlign == ContentAlignment.TopRight)
            {

                SizeF size = grx.MeasureString(label.Text, label.Font);

                int left = label.Width - (int)size.Width + label.Left;

                Rectangle rect = new Rectangle(left, label.Top, (int)size.Width, (int)size.Height);

                grx.DrawString(label.Text, label.Font, new SolidBrush(label.ForeColor), rect);

            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {

        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItemCalculate_Click(object sender, EventArgs e)
        {
            Mileage mileage = new Mileage();
            mileage.Distance = Decimal.Parse(textBoxDistance.Text);
            mileage.Fuel = Decimal.Parse(textBoxFuel.Text);
            mileage.Price = Decimal.Parse(textBoxPrice.Text);
            mileage.Tank = Decimal.Parse(textBoxTank.Text);

            mileage.Calculate();

            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat("Miles Per Gallon: {0} Mpg", mileage.MilesPerGallon.ToString("0.00")).AppendLine().AppendLine();
            stringBuilder.AppendFormat("{0} Gallons Per 100 Miles", mileage.GallonsPer100Miles.ToString("0.00")).AppendLine().AppendLine();
            stringBuilder.AppendFormat("${0} Per 100 Miles", mileage.CostPer100Miles.ToString("0.00")).AppendLine().AppendLine();
            stringBuilder.AppendFormat("Maximum Range: {0} Miles", mileage.MaximumRange.ToString("0.00"));

            textBoxResults.Text = stringBuilder.ToString();

            return;

            //frmStats statsForm = new frmStats();
            //statsForm.Mileage = mileage;
            //statsForm.ShowDialog();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            Process.Start(@"\Windows\calc.exe", "");
        }

        private void menuItemClear_Click(object sender, EventArgs e)
        {
            textBoxDistance.Text = "0";
            textBoxFuel.Text = "0";
            textBoxPrice.Text = "0";
            textBoxTank.Text = "0";

            textBoxResults.Text = string.Empty;
        }

        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            frmAbout aboutForm = new frmAbout();
            aboutForm.ShowDialog();
        }
    }
}